package com;

import com.fz.entity.Student;
import com.fz.mapper.StudentMapper;
import com.fz.mybatis.MyHelper;
import org.junit.Test;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by webrx on 2017-08-25.
 */
public class Demo {
    protected  MyHelper mh = new MyHelper();

    @Test
    public void ppp(){
        StudentMapper sdao = mh.getSession().getMapper(StudentMapper.class);
        int currpage = 3;
        int size = 2;
        int recordcount = sdao.getCount();
        int pagecount = recordcount%size==0 ? recordcount/size : recordcount/size+1;
        if(currpage<1) currpage=1;
        if(currpage>pagecount) currpage = pagecount;
        System.out.printf("当前%d页，共%d页，每页%d条，共%d条\n\n",currpage,pagecount,size,recordcount);


        int start = currpage*size-size;
        List<Student> p1 = sdao.page(start,size);
        //System.out.println(p1.size());
        for(Student st : p1){
            System.out.println(st.getName());
        }
    }



    @Test
    public void mys(){
        StudentMapper sdao = mh.getSession().getMapper(StudentMapper.class);

        //sdao.del(12);

        sdao.update(12,"李四四");
        mh.getSession().commit();

        List<Student> sts = sdao.dbs();
        System.out.println(sts.size());
        for(Student st : sts){
            System.out.println(st.getName());
        }




    }


    @Test
    public void pro(){
        /* jdbc 调用存储过程 */
        Connection conn = mh.getConn();
        try {
            CallableStatement cst = conn.prepareCall("{call dbs}");
            ResultSet rs = cst.executeQuery();
            while(rs.next()){
                System.out.println(rs.getString("name"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
